package com.sanhe.roller.activity.work;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.MenuProvider;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.sanhe.roller.R;
import com.sanhe.roller.adapter.SelectionAdapter;
import com.sanhe.roller.databinding.ActivityWorkBinding;
import com.sanhe.roller.fragment.FatFragment;
import com.sanhe.roller.fragment.Rf2Fragment;
import com.sanhe.roller.fragment.RfFragment;
import com.sanhe.roller.fragment.RollerFragment;
import com.sanhe.roller.util.Const;

public class WorkActivity extends BaseWorkActivity {

    ActivityWorkBinding binding;
    private RecyclerView mRecyclerView;
    private LinearLayoutManager slideManager;
    private LinearLayoutManager disableManager;
    private SelectionAdapter adapter;

    private Fragment currentFragment = new Fragment();
    private RollerFragment rollerFragment = new RollerFragment();
    private RfFragment rfFragment = new RfFragment();
    private Rf2Fragment rf2Fragment = new Rf2Fragment();
    private FatFragment fatFragment = new FatFragment();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityWorkBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        mRecyclerView = binding.recyclerView;
        adapter = new SelectionAdapter(this);
        slideManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        disableManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }

            @Override
            public boolean canScrollHorizontally() {
                return false;
            }
        };
        enableSlide();
        initSlide();

        initScrollListener();
        switchFragment(getFragment()).commit();

        binding.day.setText("12d 12h 12m");

        binding.back.setOnClickListener(v -> finish());
    }


    private void initScrollListener() {

        binding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            int itemCount_page = adapter.getMeaSureItemCount();

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    RecyclerView.LayoutManager manager = mRecyclerView.getLayoutManager();
                    if (manager instanceof LinearLayoutManager) {
                        LinearLayoutManager linearManager = (LinearLayoutManager) manager;
                        //获取最后一个可见view的位置
                        int lastItemPosition = linearManager.findLastVisibleItemPosition();
                        //获取第一个可见view的位置
                        int firstItemPosition = linearManager.findFirstVisibleItemPosition();
                        if (lastItemPosition - firstItemPosition >= itemCount_page) {
                            return;
                        }
                        if (Const.mode == firstItemPosition + itemCount_page / 2) {
                            return;
                        }
                        Const.mode = firstItemPosition + itemCount_page / 2;
                        switchFragment(getFragment()).commit();
                        Log.e(TAG, "center: " + (firstItemPosition + itemCount_page / 2));
                        Log.e(TAG, firstItemPosition + "  ======  " + lastItemPosition + " ==== " + itemCount_page);
                    }
                }
            }
        });
    }

    private FragmentTransaction switchFragment(Fragment targetFragment) {

        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        if (!targetFragment.isAdded()) {
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
            transaction.add(R.id.frame, targetFragment, targetFragment.getClass().getName());

        } else {
            transaction
                    .hide(currentFragment)
                    .show(targetFragment);
        }
        currentFragment = targetFragment;
        return transaction;
    }

    private Fragment getFragment() {
        switch (Const.mode) {
            case 2:
                return rfFragment;
            case 3:
                return rf2Fragment;
            case 4:
                return fatFragment;
            default:
                return rollerFragment;
        }
    }

    private void enableSlide(){
        mRecyclerView.setLayoutManager(slideManager);
        LinearSnapHelper mLinearSnapHelper = new LinearSnapHelper();
        mLinearSnapHelper.attachToRecyclerView(mRecyclerView);
        mRecyclerView.setAdapter(adapter);
    }

    private void disableSlide(){
        mRecyclerView.setLayoutManager(disableManager);
        mRecyclerView.setAdapter(adapter);
    }


    private void initSlide() {
        slideManager.scrollToPosition(Const.mode - 1);
    }

    @Override
    public void addMenuProvider(@NonNull MenuProvider provider, @NonNull LifecycleOwner owner, @NonNull Lifecycle.State state) {

    }
}
